<template>
	<view class="wrap">
		<view class="content">
			<u-form :model="form" ref="uForm">
				<view class="title">用户注册</view>
				<u-form-item prop="username">
					<u-input v-model="form.username" type="text" placeholder="请输入用户名" />
				</u-form-item>
				<u-form-item prop="password">
					<u-input v-model="form.password" type="password" placeholder="请输入密码" />
				</u-form-item>
				<u-form-item prop="pwd2">
					<u-input v-model="form.pwd2" type="password" placeholder="请再次输入密码" />
				</u-form-item>
				<button @tap="register" class="register-btn">立即注册</button>
			</u-form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: '',
					pwd2: ''
				}
			}
		},
		methods: {
			async register() {
				if (!this.form.username) {
					this.$u.toast('请输入用户名')
					return
				}

				if (!this.form.password) {
					this.$u.toast('请输入密码')
					return
				}

				if (!this.form.pwd2) {
					this.$u.toast('请再次输入密码')
					return
				}

				if (this.form.password !== this.form.pwd2) {
					this.$u.toast('两次输入的密码不一致')
					return
				}

				let res = await uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'register',
						params: this.form,
					}
				})
				if (res.result.code === 0) {
					this.$u.toast('注册成功');
					this.$u.route('/pages/index/index');
				} else {
					this.$u.toast(res.result.msg);
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	/* 在你的样式文件中添加以下CSS代码 */

	/* 包裹整个表单的容器 */
	.wrap {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}

	/* 表单内容区域 */
	.content {
		width: 300px;
		padding: 20px;
		background-color: #f5f5f5;
		border-radius: 8px;
		box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
	}

	/* 标题样式 */
	.title {
		font-size: 18px;
		font-weight: bold;
		text-align: center;
		margin-bottom: 20px;
	}

	/* 输入框样式 */
	.u-input {
		width: 100%;
		margin-bottom: 15px;
		border: 1px solid #ccc; /* 添加边框 */
	}

	/* 注册按钮样式 */
	.register-btn {
		width: 100%;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 4px;
		padding: 10px;
		font-size: 16px;
		cursor: pointer;
		transition: background-color 0.3s ease;
	}

	.register-btn:hover {
		background-color: #0056b3;
	}
	.register-text:hover {
		text-decoration: underline;
	}
</style>